package fcl.mylife;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import fcl.platform.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

public class Downloader {

	public Downloader() {
	}

	Socket s = null;
	PrintWriter out = null;
	BufferedReader in = null;
	String result1 = "";
	String result = "";
	
	Context context = null;

	BufferedReader reader=null;

	public void connect(String name, int port) throws UnknownHostException,
			IOException {

		s = new Socket(name, port);
		out = new PrintWriter(s.getOutputStream(), true);
	//	in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
	//	in = new BufferedReader(new InputStreamReader(s.getInputStream()));
		
	}
	
	public void setContext(Context c){
		this.context = c;
	}
	
	

	/**
	 * Sends a string message to the server.
	 * 
	 * @param msg
	 *            The message to be sent.
	 * @throws IOException
	 */
	public void send(String msg) throws IOException {
		if (!s.isClosed() && msg != null) {
			out.println(msg);
		//	out.close();
		}
	}

	public void receive() throws IOException {
		
	
		if (!s.isClosed()) {
			
			InputStream inputStream= s.getInputStream();
			
			reader = new BufferedReader(new InputStreamReader(inputStream));
			
			result1 = reader.readLine();
			
			String filepath = Environment.getExternalStorageDirectory()
					+ "//fcl//download.csv";
			
			
			File f = new File(filepath); 
	
			PrintWriter pw = new PrintWriter(f);

			// write to file and save on the local disk
            while(result1 != null)
			{
				// Note the "\\" used in the path of the file
				// instead of "\", this is required to read
				// the path in the String format.
				// Write to file for the first row
				pw.println(result1);
				result1 = reader.readLine();
				

			}
            
         // Flush the output to the file
			pw.flush();

			// Close the Print Writer
			pw.close();

			// Close the File Writer
		

			out.close();
			reader.close();
			s.close();

		}

	}

	// update the data
	public void requestData() {
		// TODO Auto-generated method stub
		try {
			connect("129.132.32.159", 8080);
	//		send("request request = "+ get_client_id());// send userid
			send("request request = 4860804");
			
			receive();
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	//	Requester client = new Requester();
	//	client.run();

	}
	
	
	
	
	private String get_client_id() {
		
		if(context == null){
			return "";
		}
		SharedPreferences settings = context.getSharedPreferences(context.getString(R.string.pref_name), 0);
		String client_id = settings.getString(context.getString(R.string.client_id_key), null);
		
		if(client_id == null) {
			client_id = java.util.UUID.randomUUID().toString();
			SharedPreferences.Editor editor = settings.edit();
			editor.putString(context.getString(R.string.client_id_key), client_id);
			editor.commit();
		}
		return client_id;
	}
	

}
